package com.totsp.conference;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.totsp.conference.data.DataHelper;
import com.totsp.conference.data.Speaker;

import java.util.ArrayList;

public class SpeakerList extends ConfActivity {

   private ArrayList<Speaker> speakers;

   private ListView listView;
   private SpeakerAdapter adapter;
   private TextView title;

   private ProgressDialog progressDialog;

   @Override
   public void onCreate(final Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.list_layout);

      progressDialog = new ProgressDialog(this);
      progressDialog.setMessage("Retrieving data...");
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      progressDialog.setCancelable(false);

      listView = (ListView) findViewById(R.id.listview);
      listView.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {
            Intent intent = new Intent(SpeakerList.this, SpeakerDetail.class);
            intent.putExtra(Constants.SPKR_NAME, SpeakerList.this.speakers.get(index).fName + " "
                     + SpeakerList.this.speakers.get(index).lName);
            intent.putExtra(Constants.BIO, SpeakerList.this.speakers.get(index).bio);
            SpeakerList.this.startActivity(intent);
         }
      });

      speakers = new ArrayList<Speaker>();
      adapter = new SpeakerAdapter(this, 0, speakers);
      listView.setAdapter(adapter);

      title = (TextView) findViewById(R.id.listtitle);
      title.setText(R.string.speakers_title);
   }

   @Override
   public void onResume() {
      super.onResume();
      if (!app.getSpeakersCache().isEmpty() && speakers.isEmpty()) {
         speakers.clear();
         speakers.addAll(app.getSpeakersCache());
      } else if (speakers.isEmpty()) {
         if (app.connectionPresent()) {
            new BackgroundTask().execute();
         } else {
            Toast.makeText(this, "Network not available, cannot retrieve data", Toast.LENGTH_SHORT).show();
         }
      }
   }

   @Override
   protected void onPause() {
      if (progressDialog.isShowing()) {
         progressDialog.dismiss();
      }
      super.onPause();
   }

   private class SpeakerAdapter extends ArrayAdapter<Speaker> {

      LayoutInflater vi = (LayoutInflater) SpeakerList.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      public SpeakerAdapter(final Context context, final int resId, final ArrayList<Speaker> speakers) {
         super(context, resId, speakers);
      }

      @Override
      public View getView(final int position, final View convertView, final ViewGroup parent) {
         // re-use the convertView, try not to recreate objects here or
         // inflate every time (expensive)
         View v = convertView;
         if (v == null) {
            v = vi.inflate(R.layout.listviewitem, null);
         }

         Speaker s = speakers.get(position);
         if (s != null) {
            ((TextView) v.findViewById(R.id.list_item_above)).setText(s.fName + " " + s.lName);
            ((TextView) v.findViewById(R.id.list_item_below)).setText(s.bio);
         }
         return v;
      }
   }

   private class BackgroundTask extends AsyncTask<Void, Void, ArrayList<Speaker>> {

      @Override
      protected void onPreExecute() {
         if (!progressDialog.isShowing()) {
            progressDialog.show();
         }
      }

      @Override
      protected ArrayList<Speaker> doInBackground(Void... args) {
         return DataHelper.getSpeakers();
      }

      @Override
      protected void onPostExecute(ArrayList<Speaker> newSpeakers) {
         if (progressDialog.isShowing()) {
            progressDialog.hide();
         }

         if (newSpeakers != null && !newSpeakers.isEmpty()) {
            speakers.clear();
            speakers.addAll(newSpeakers);
            app.setSpeakersCache(newSpeakers);
            adapter.notifyDataSetChanged();
         }
      }
   }
}